package com.wind.trademark.user.dao.impl;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class UserSql {
	public static String queryUserSQL( Map<String,Object> params){
		
		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("SELECT DISTINCT user.* FROM acc_user user ");
		stringBuffer.append("LEFT  JOIN  acc_user_role acc_user_role ON user.ID = acc_user_role.USER_ID ");
		stringBuffer.append("WHERE 1=1 ");

		if(StringUtils.isNotEmpty((String) params.get("account"))){
			stringBuffer.append("AND user.account LIKE :account ");
			params.put("account", "%"+params.get("account").toString().toLowerCase()+"%");
		}
		if(StringUtils.isNotEmpty((String) params.get("name"))){
			stringBuffer.append("AND user.name LIKE :name ");
			params.put("name", "%"+params.get("name").toString().toLowerCase()+"%");
		}
		if(StringUtils.isNotEmpty((String) params.get("contact"))){
			stringBuffer.append("AND ( user.mobile LIKE :contact or user.email LIKE :contact ) ");
			params.put("contact", "%"+params.get("contact").toString().toLowerCase()+"%");
		}
		if(params.get("roles")!=null&&StringUtils.isNotEmpty(params.get("roles").toString())){
			String[] roles=params.get("roles").toString().split(",");
			params.put("roles", Arrays.asList(roles));
//			params.put("roles", "("+params.get("roles")+")" );
			stringBuffer.append("AND acc_user_role.ROLE_ID IN (:roles) ");		
		}
		stringBuffer.append("ORDER BY user.create_time ");
		return stringBuffer.toString();
	}

	public static String checkAccountUsed(Map<String, Object> params) {
		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("SELECT DISTINCT user.* FROM acc_user user ");
 		stringBuffer.append(" WHERE 1=1 ");
		if(StringUtils.isNotEmpty((String) params.get("account"))){
			stringBuffer.append(" AND user.account = :account ");
 		}
		if(StringUtils.isNotEmpty((String) params.get("id"))){
			stringBuffer.append(" AND user.id != :id ");
 		}
 		return stringBuffer.toString();
	}
}
